/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package UI;

import CBD.Citas;
import CBD.Contacto;
import Negocios.ControlCitas;
import Negocios.ControlContacto;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Dany
 */
public class Calendario extends javax.swing.JInternalFrame {

    private String nombreMes = "";
    private int numeroMesActual = 0;
    private int numeroMes = 0;
    private int numeroEspacios = 0;
    private int year = 0;
    private JLabel jlDia[];
    private JLabel jlNumCitas[];
    private Date fecha = new Date();

    /**
     * Creates new form Citas
     */
    public Calendario() {
        initComponents();
        inicializaCalendario();
        //crearContacto();
        //crearCita();65
    }

    //Preprara el panel de calendario
    private void inicializaCalendario() {
        //Prepara los paneles
        jpCalendario.setVisible(false);
        jpCalendario.removeAll();
        jdtFechaABuscar.setDate(fecha);
        jdtFechaCita.setDate(fecha);
        //Asigna organizacion
        organizaLayouts(jpCalendario);
        //Genera los campos para los precios
        generaPanelDias();
        //Muestra los cambios realizados
        jpCalendario.setVisible(true);
    }

    private void limpiarCampos() {
        jcbTipoContacto.setSelectedIndex(0);
        jdtFechaCita.setDate(new Date());
        jtfBuscar.setText("");
        jtaDescripcion.setText("");
    }

    //Establece la organizacion de los componentes de un panel
    private void organizaLayouts(JPanel panel) {
        panel.setLayout(new GridLayout(6, 7, 5, 5));
    }

    private int getListaCitas(Date fecha) {
        int numCitas = 0;
        ControlCitas cc = new ControlCitas();
        List listaCitas = cc.select("from Citas");
        numCitas = getNumCitasDia(fecha, listaCitas);
        return numCitas;
    }
    /*Genera paneles de acuerdo a los dias del nombreMes*/

    private int getNumCitasDia(Date fecha, List listaCitas) {
        int numCitas = 0;
        for (Iterator it = listaCitas.iterator(); it.hasNext();) {
            Citas cita = (Citas) it.next();
            SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
            if (formateador.format(fecha).equals(formateador.format(cita.getFecha()))) {
                numCitas++;
            }
        }
        return numCitas;
    }

    private void crearContacto() {
        Contacto c = new Contacto();
        c.setNombre("Daniel");
        c.setTipo("Proveedor");
        c.setEmail("isc.daniel.ruiz@gmail.com");
        c.setTelefono("4570713");
        ControlContacto cc = new ControlContacto();
        cc.add(c);
    }

    private void crearCita() {
        if (jtContactos.getSelectedRow() != -1) {
            Citas cita = new Citas();
            ControlContacto cc = new ControlContacto();
            //Busco el contacto seleccionado en la tabla
            String nombre = jtContactos.getValueAt(jtContactos.getSelectedRow(), 1).toString();
            Contacto contacto = (Contacto) cc.select("from Contacto where nombre = '" + nombre + "'").get(0);
            //--------------Construyendo el Contacto---------------------------------------
            cita.setContacto(contacto);
            cita.setDescripcion(jtaDescripcion.getText());
            cita.setFecha(jdtFechaCita.getDate());//------------es la fecha elegida en el jdchooser
            cita.setHora(jcbHoraCita.getSelectedItem().toString());
            cita.setMinuto(jcbMinutosCita.getSelectedItem().toString());
            //------------------Guardo los cambios muajaja---------------------------------
            ControlCitas cci = new ControlCitas();
            cci.add(cita);
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un contacto de la lista de contactos",
                    "No ha seleccionado un contacto", 2);
        }
    }

    private void generaPanelDias() {
        //obtengo los dias del nombreMes seleccionado
        int dias = getNumeroDiasMes();
        int extras = (42 - numeroEspacios) - dias;
        //Inicializa los objetos antes de generarlos
        JPanel jpMarcoDia[] = new JPanel[dias + 1];
        JPanel jpSpaceWhite[] = new JPanel[numeroEspacios];
        JPanel jpSpaceWhiteE[] = new JPanel[extras];
        jlDia = new JLabel[dias + 1];
        jlNumCitas = new JLabel[dias + 1];
        for (int i = 0; i < numeroEspacios; i++) {
            //Propiedades del panel jpMarcoDia
            jpSpaceWhite[i] = new JPanel();
            jpSpaceWhite[i].setBackground(new java.awt.Color(112, 40, 4));
            jpSpaceWhite[i].setSize(100, 100);
            jpSpaceWhite[i].setLayout(new GridLayout(2, 1, 3, 3));
            jpSpaceWhite[i].setVisible(true);
            //Colocando todo en el panel
            jpCalendario.add(jpSpaceWhite[i]);
        }
        //Creo y ensamblo los nuevos objetos
        for (int i = 1; i < dias + 1; i++) {
            final int indice = i;
            //Propiedades de la etiqueta jlDia
            jlDia[i] = new JLabel();
            jlDia[i].setBackground(new java.awt.Color(153, 102, 0));
            jlDia[i].setForeground(Color.WHITE);
            jlDia[i].setOpaque(true);
            jlDia[i].setSize(
                    100, 100);
            jlDia[i].setFont(new java.awt.Font("Tahoma", 1, 44));
            jlDia[i].setText(
                    String.valueOf(i));
            jlDia[i].setHorizontalAlignment(SwingConstants.CENTER);
            jlDia[i].setVisible(
                    true);
            //Propiedades de la etiqueta jlNumCitas
            jlNumCitas[i] = new JLabel();
            jlNumCitas[i].setBackground(new java.awt.Color(153, 102, 0));
            jlNumCitas[i].setForeground(Color.white);
            jlNumCitas[i].setOpaque(true);
            jlNumCitas[i].setSize(
                    47, 17);
            jlNumCitas[i].setFont(new java.awt.Font("Tahoma", 1, 18));
            jlNumCitas[i].setHorizontalAlignment(SwingConstants.CENTER);
            jlNumCitas[i].setText(
                    "0 Citas");
            jlNumCitas[i].setVisible(
                    true);

            //Propiedades del panel jpMarcoDia
            jpMarcoDia[i] = new JPanel();
            jpMarcoDia[i].setBackground(new java.awt.Color(153, 102, 0));
            jpMarcoDia[i].setSize(100, 100);
            jpMarcoDia[i].setLayout(new GridLayout(2, 1, 3, 3));

            //Busca las citas que coincidan con la fecha
            Date fecha = deStringToDate(jlDia[indice].getText() + "-" + numeroMes + "-" + year);
            int numCitas = getListaCitas(fecha);
            jlNumCitas[indice].setText("Citas: " + numCitas);
            if (numCitas > 0) {
                jlNumCitas[indice].setBackground(new java.awt.Color(255, 100, 0));
                jlDia[indice].setForeground(Color.yellow);
                jlDia[indice].setBackground(new java.awt.Color(255, 100, 0));
                jpMarcoDia[indice].setBackground(new java.awt.Color(255, 100, 0));
            }
            //Eventos para las etiquetas
            jlDia[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    setEventoComponente(evt, indice);
                }
            });
            jlNumCitas[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    setEventoComponente(evt, indice);
                }
            });
            jpMarcoDia[i].addMouseListener(new java.awt.event.MouseAdapter() {
                @Override
                public void mouseClicked(java.awt.event.MouseEvent evt) {
                    setEventoComponente(evt, indice);
                }
            });

            //Colocando los objetos
            jpMarcoDia[i].add(jlDia[i], BorderLayout.CENTER);
            jpMarcoDia[i].add(jlNumCitas[i], BorderLayout.SOUTH);
            jpMarcoDia[i].setVisible(true);
            //Colocando todo en el panel
            jpCalendario.add(jpMarcoDia[i]);
        }

        for (int i = 0; i < extras; i++) {
            //Propiedades del panel jpMarcoDia
            jpSpaceWhiteE[i] = new JPanel();
            jpSpaceWhiteE[i].setBackground(new java.awt.Color(112, 40, 4));
            jpSpaceWhiteE[i].setSize(100, 300);
            jpSpaceWhiteE[i].setLayout(new GridLayout(2, 1, 3, 3));
            jpSpaceWhiteE[i].setVisible(true);
            //Colocando todo en el panel
            jpCalendario.add(jpSpaceWhiteE[i]);
        }

    }

    private void setEventoComponente(java.awt.event.MouseEvent evt, final int indice) {
        //Manda el foco a una tab y la muestra
        jtpAgenda.setEnabledAt(2, true);
        jtpAgenda.setSelectedIndex(2);
        //Busca las citas que coincidan con la fecha
        fecha = deStringToDate(jlDia[indice].getText() + "-" + numeroMes + "-" + year);
        jdtFechaABuscar.setDate(fecha);
    }

    private int getPrimerDiaMes() {
        int primerDiaMes = 0;
        int M = 0;
        M = modulo(numeroMes, (getAnioBisiesto(year)));
        /*Ejemplo: 0 = Domingo, 1 = Lunes, 2 = Martes, etc.*/
        primerDiaMes = ((year - 1) % 7 + (((year - 1) / 4) - 3 * ((((year - 1) / 100) + 1) / 4)) % 7 + M + 1 % 7) % 7;
        if (primerDiaMes > 0) {
            primerDiaMes--;
        } else {
            primerDiaMes += 6;
        }
        return primerDiaMes;
    }

    public static int modulo(int numeroMes, boolean esBisiesto) {
        if (esBisiesto) {
            switch (numeroMes) {
                case 1:
                    return 0;
                case 2:
                    return 3;
                case 3:
                    return 3;
                case 4:
                    return 6;
                case 5:
                    return 1;
                case 6:
                    return 4;
                case 7:
                    return 6;
                case 8:
                    return 2;
                case 9:
                    return 5;
                case 10:
                    return 0;
                case 11:
                    return 3;
                case 12:
                    return 5;
            }
            return 0;
        }
        switch (numeroMes) {
            case 1:
                return 1;
            case 2:
                return 3;
            case 3:
                return 4;
            case 4:
                return 0;
            case 5:
                return 2;
            case 6:
                return 5;
            case 7:
                return 0;
            case 8:
                return 3;
            case 9:
                return 6;
            case 10:
                return 1;
            case 11:
                return 4;
            case 12:
                return 6;
        }
        return 0;
    }

    private int getNumeroDiasMes() {
        int dias = 0;

        if (numeroMesActual != 0) {
            numeroEspacios = getPrimerDiaMes();
            Calendar cal = new GregorianCalendar(year, numeroMes, 1);
            // Get the number of days in that month 
            dias = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        } else {
            Date ahora = new Date();
            SimpleDateFormat formato;
            formato = new SimpleDateFormat("M");
            numeroMes = Integer.parseInt(formato.format(ahora));
            formato = new SimpleDateFormat("Y");
            year = Integer.parseInt(formato.format(ahora));
            numeroEspacios = getPrimerDiaMes();
        }

        switch (numeroMes) {
            case 1:
                dias = 31;
                nombreMes = "Enero";
                break;
            case 3:
                dias = 31;
                nombreMes = "Marzo";
                break;
            case 5:
                dias = 31;
                nombreMes = "Mayo";
                break;
            case 7:
                dias = 31;
                nombreMes = "Julio";
                break;
            case 8:
                dias = 31;
                nombreMes = "Agosto";
                break;
            case 10:
                dias = 31;
                nombreMes = "Octubre";
                break;
            case 12:
                dias = 31;
                nombreMes = "Diciembre";
                break;
            case 2:
                if (getAnioBisiesto(year)) {
                    dias = 29;
                } else {
                    dias = 28;
                }
                nombreMes = "Febrero";
                break;
            case 4:
                dias = 30;
                nombreMes = "Abril";
                break;
            case 6:
                dias = 30;
                nombreMes = "Junio";
                break;
            case 9:
                dias = 30;
                nombreMes = "Septiembre";
                break;
            case 11:
                dias = 30;
                nombreMes = "Noviembre";
                break;
        }
        jlNombreMes.setText(nombreMes);
        return dias;
    }

    public static boolean getAnioBisiesto(int year) {
        if ((year % 100 == 0 && year % 400 == 0) || (year % 100 != 0 && year % 400 == 0)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel5 = new javax.swing.JLabel();
        jtpAgenda = new javax.swing.JTabbedPane();
        jspCalendario = new javax.swing.JScrollPane();
        jpContenedorCalendario = new javax.swing.JPanel();
        jpCalendario = new javax.swing.JPanel();
        jlLunes = new javax.swing.JLabel();
        jlMartes = new javax.swing.JLabel();
        jlMiercoles = new javax.swing.JLabel();
        jlJueves = new javax.swing.JLabel();
        jlViernes = new javax.swing.JLabel();
        jlSabado = new javax.swing.JLabel();
        jlDomingo = new javax.swing.JLabel();
        jpMes = new javax.swing.JPanel();
        jlNombreMes = new javax.swing.JLabel();
        jlAdelanteMes = new javax.swing.JLabel();
        jlAtrasMEs = new javax.swing.JLabel();
        jspCitas = new javax.swing.JScrollPane();
        jpNuevaCita = new javax.swing.JPanel();
        jtfBuscar = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        jtContactos = new javax.swing.JTable();
        jdtFechaCita = new com.toedter.calendar.JDateChooser();
        jcbTipoContacto = new javax.swing.JComboBox();
        jcbHoraCita = new javax.swing.JComboBox();
        jcbMinutosCita = new javax.swing.JComboBox();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator2 = new javax.swing.JSeparator();
        jlHoraEvento = new javax.swing.JLabel();
        jlListaContacto = new javax.swing.JLabel();
        jbCrearCita = new javax.swing.JButton();
        jbRestablecerCampos = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtaDescripcion = new javax.swing.JTextArea();
        jlHoraEvento1 = new javax.swing.JLabel();
        jlListaContacto1 = new javax.swing.JLabel();
        jSeparator4 = new javax.swing.JSeparator();
        jSeparator5 = new javax.swing.JSeparator();
        jpEditarEliminarCita = new javax.swing.JPanel();
        jbEliminar = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        jtCitas = new javax.swing.JTable();
        jSeparator3 = new javax.swing.JSeparator();
        jlTipoContacto = new javax.swing.JLabel();
        jbEditar = new javax.swing.JButton();
        jtBuscarCitas = new javax.swing.JTextField();
        jdtFechaABuscar = new com.toedter.calendar.JDateChooser();

        jLabel5.setText("jLabel5");

        setBackground(new java.awt.Color(153, 153, 153));
        setClosable(true);
        setForeground(new java.awt.Color(255, 252, 252));
        setIconifiable(true);

        jtpAgenda.setBackground(new java.awt.Color(112, 40, 4));
        jtpAgenda.setTabPlacement(javax.swing.JTabbedPane.LEFT);

        jpContenedorCalendario.setBackground(new java.awt.Color(112, 40, 4));
        jpContenedorCalendario.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpContenedorCalendarioComponentShown(evt);
            }
        });

        jpCalendario.setBackground(new java.awt.Color(112, 40, 4));
        jpCalendario.setAutoscrolls(true);
        jpCalendario.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpCalendarioComponentShown(evt);
            }
        });

        javax.swing.GroupLayout jpCalendarioLayout = new javax.swing.GroupLayout(jpCalendario);
        jpCalendario.setLayout(jpCalendarioLayout);
        jpCalendarioLayout.setHorizontalGroup(
            jpCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 758, Short.MAX_VALUE)
        );
        jpCalendarioLayout.setVerticalGroup(
            jpCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 607, Short.MAX_VALUE)
        );

        jlLunes.setBackground(new java.awt.Color(51, 51, 51));
        jlLunes.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlLunes.setForeground(new java.awt.Color(255, 252, 252));
        jlLunes.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlLunes.setText("Domingo");
        jlLunes.setOpaque(true);

        jlMartes.setBackground(new java.awt.Color(51, 51, 51));
        jlMartes.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlMartes.setForeground(new java.awt.Color(255, 252, 252));
        jlMartes.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlMartes.setText("Lunes");
        jlMartes.setOpaque(true);

        jlMiercoles.setBackground(new java.awt.Color(51, 51, 51));
        jlMiercoles.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlMiercoles.setForeground(new java.awt.Color(255, 252, 252));
        jlMiercoles.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlMiercoles.setText("Martes");
        jlMiercoles.setOpaque(true);

        jlJueves.setBackground(new java.awt.Color(51, 51, 51));
        jlJueves.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlJueves.setForeground(new java.awt.Color(255, 252, 252));
        jlJueves.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlJueves.setText("Miercoles");
        jlJueves.setOpaque(true);

        jlViernes.setBackground(new java.awt.Color(51, 51, 51));
        jlViernes.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlViernes.setForeground(new java.awt.Color(255, 252, 252));
        jlViernes.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlViernes.setText("Jueves");
        jlViernes.setOpaque(true);

        jlSabado.setBackground(new java.awt.Color(51, 51, 51));
        jlSabado.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlSabado.setForeground(new java.awt.Color(255, 252, 252));
        jlSabado.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlSabado.setText("Viernes");
        jlSabado.setOpaque(true);

        jlDomingo.setBackground(new java.awt.Color(51, 51, 51));
        jlDomingo.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jlDomingo.setForeground(new java.awt.Color(255, 252, 252));
        jlDomingo.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jlDomingo.setText("Sabado");
        jlDomingo.setOpaque(true);

        jpMes.setBackground(new java.awt.Color(0, 0, 0));
        jpMes.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpMesComponentShown(evt);
            }
        });

        jlNombreMes.setBackground(java.awt.Color.white);
        jlNombreMes.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jlNombreMes.setForeground(java.awt.Color.white);

        jlAdelanteMes.setBackground(java.awt.Color.white);
        jlAdelanteMes.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jlAdelanteMes.setForeground(new java.awt.Color(255, 204, 51));
        jlAdelanteMes.setText(">");
        jlAdelanteMes.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlAdelanteMesMouseClicked(evt);
            }
        });

        jlAtrasMEs.setBackground(java.awt.Color.white);
        jlAtrasMEs.setFont(new java.awt.Font("Tahoma", 0, 24)); // NOI18N
        jlAtrasMEs.setForeground(new java.awt.Color(255, 204, 51));
        jlAtrasMEs.setText("<");
        jlAtrasMEs.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jlAtrasMEsMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout jpMesLayout = new javax.swing.GroupLayout(jpMes);
        jpMes.setLayout(jpMesLayout);
        jpMesLayout.setHorizontalGroup(
            jpMesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpMesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlAtrasMEs)
                .addGap(262, 262, 262)
                .addComponent(jlNombreMes, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jlAdelanteMes)
                .addGap(20, 20, 20))
        );
        jpMesLayout.setVerticalGroup(
            jpMesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jlNombreMes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jpMesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jlAdelanteMes)
                .addComponent(jlAtrasMEs))
        );

        javax.swing.GroupLayout jpContenedorCalendarioLayout = new javax.swing.GroupLayout(jpContenedorCalendario);
        jpContenedorCalendario.setLayout(jpContenedorCalendarioLayout);
        jpContenedorCalendarioLayout.setHorizontalGroup(
            jpContenedorCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jpMes, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(jpContenedorCalendarioLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpContenedorCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jpCalendario, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jpContenedorCalendarioLayout.createSequentialGroup()
                        .addComponent(jlLunes, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlMartes, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlMiercoles, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlJueves, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlViernes, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlSabado, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlDomingo, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        jpContenedorCalendarioLayout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jlDomingo, jlJueves, jlLunes, jlMartes, jlMiercoles, jlSabado, jlViernes});

        jpContenedorCalendarioLayout.setVerticalGroup(
            jpContenedorCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpContenedorCalendarioLayout.createSequentialGroup()
                .addComponent(jpMes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpContenedorCalendarioLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jlMiercoles, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlJueves, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlViernes, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlDomingo, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlSabado, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlMartes, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jlLunes, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jpCalendario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jpContenedorCalendarioLayout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {jlDomingo, jlJueves, jlLunes, jlMartes, jlMiercoles, jlSabado, jlViernes});

        jpCalendario.getAccessibleContext().setAccessibleName("");

        jspCalendario.setViewportView(jpContenedorCalendario);

        jtpAgenda.addTab("Calendario", jspCalendario);

        jspCitas.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jspCitasComponentShown(evt);
            }
        });

        jpNuevaCita.setBackground(new java.awt.Color(112, 40, 4));
        jpNuevaCita.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpNuevaCitaComponentShown(evt);
            }
        });

        jtfBuscar.setBorder(javax.swing.BorderFactory.createTitledBorder("Caja de busqueda"));
        jtfBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtfBuscarKeyTyped(evt);
            }
        });

        jtContactos.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Id", "Nombre", "Tipo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(jtContactos);
        if (jtContactos.getColumnModel().getColumnCount() > 0) {
            jtContactos.getColumnModel().getColumn(0).setMaxWidth(50);
            jtContactos.getColumnModel().getColumn(2).setMaxWidth(150);
        }

        jcbTipoContacto.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos", "Proveedor", "Cliente", "Empleado", "Otros" }));
        jcbTipoContacto.setBorder(javax.swing.BorderFactory.createTitledBorder("Tipo de contacto"));
        jcbTipoContacto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jcbTipoContactoActionPerformed(evt);
            }
        });

        jcbHoraCita.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "00:00", "01:00", "02:00", "03:00", "04:00", "05:00", "06:00", "07:00", "08:00", "09:00", "10:00", "11:00", "12:00", "13:00", "14:00", "15:00", "16:00", "17:00", "18:00", "19:00", "20:00", "21:00", "22:00", "23:00" }));

        jcbMinutosCita.setModel(new javax.swing.DefaultComboBoxModel(new String[] { ":00", ":15", ":30", ":45" }));

        jlHoraEvento.setBackground(new java.awt.Color(51, 51, 51));
        jlHoraEvento.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlHoraEvento.setForeground(new java.awt.Color(255, 255, 255));
        jlHoraEvento.setText("Descripción del evento");
        jlHoraEvento.setOpaque(true);

        jlListaContacto.setBackground(new java.awt.Color(51, 51, 51));
        jlListaContacto.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlListaContacto.setForeground(new java.awt.Color(255, 255, 255));
        jlListaContacto.setText("Libreta de contactos - Se muestran por tipo, seleccione alguno para hacer una cita");
        jlListaContacto.setOpaque(true);

        jbCrearCita.setText("Crear cita");
        jbCrearCita.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCrearCitaActionPerformed(evt);
            }
        });

        jbRestablecerCampos.setText("Restablecer");
        jbRestablecerCampos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbRestablecerCamposActionPerformed(evt);
            }
        });

        jtaDescripcion.setColumns(20);
        jtaDescripcion.setRows(5);
        jScrollPane1.setViewportView(jtaDescripcion);

        jlHoraEvento1.setBackground(new java.awt.Color(51, 51, 51));
        jlHoraEvento1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlHoraEvento1.setForeground(new java.awt.Color(255, 255, 255));
        jlHoraEvento1.setText("Horas y minutos");
        jlHoraEvento1.setOpaque(true);

        jlListaContacto1.setBackground(new java.awt.Color(51, 51, 51));
        jlListaContacto1.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlListaContacto1.setForeground(new java.awt.Color(255, 255, 255));
        jlListaContacto1.setText("Calendario - seleccione un dia para hacer una cita, por defecto es la fecha actual");
        jlListaContacto1.setOpaque(true);

        javax.swing.GroupLayout jpNuevaCitaLayout = new javax.swing.GroupLayout(jpNuevaCita);
        jpNuevaCita.setLayout(jpNuevaCitaLayout);
        jpNuevaCitaLayout.setHorizontalGroup(
            jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                        .addComponent(jdtFechaCita, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                        .addGroup(jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator5, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlHoraEvento, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                                .addComponent(jcbTipoContacto, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jtfBuscar))
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlListaContacto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jlListaContacto1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jSeparator4, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jlHoraEvento1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpNuevaCitaLayout.createSequentialGroup()
                                .addComponent(jcbHoraCita, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jcbMinutosCita, javax.swing.GroupLayout.PREFERRED_SIZE, 208, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addComponent(jSeparator2, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jbCrearCita)
                                .addGap(37, 37, 37)
                                .addComponent(jbRestablecerCampos)))
                        .addGap(31, 31, 31))))
        );
        jpNuevaCitaLayout.setVerticalGroup(
            jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpNuevaCitaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlListaContacto1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator4, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdtFechaCita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addComponent(jlHoraEvento1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator2, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbHoraCita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbMinutosCita, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jlListaContacto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcbTipoContacto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 116, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(22, 22, 22)
                .addComponent(jlHoraEvento)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator5, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(7, 7, 7)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jpNuevaCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCrearCita)
                    .addComponent(jbRestablecerCampos))
                .addContainerGap(385, Short.MAX_VALUE))
        );

        jspCitas.setViewportView(jpNuevaCita);

        jtpAgenda.addTab("Crear cita", jspCitas);

        jpEditarEliminarCita.addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                jpEditarEliminarCitaComponentShown(evt);
            }
        });

        jbEliminar.setText("Eliminar");

        jtCitas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "id", "Nombre", "Fecha", "Hora", "Minutos"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane3.setViewportView(jtCitas);
        if (jtCitas.getColumnModel().getColumnCount() > 0) {
            jtCitas.getColumnModel().getColumn(0).setMaxWidth(50);
            jtCitas.getColumnModel().getColumn(2).setMaxWidth(150);
            jtCitas.getColumnModel().getColumn(3).setMaxWidth(100);
            jtCitas.getColumnModel().getColumn(4).setMaxWidth(100);
        }

        jlTipoContacto.setBackground(new java.awt.Color(51, 51, 51));
        jlTipoContacto.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jlTipoContacto.setForeground(new java.awt.Color(255, 255, 255));
        jlTipoContacto.setText("Citas - muestra las citas pendientes");
        jlTipoContacto.setOpaque(true);

        jbEditar.setText("Editar");

        jtBuscarCitas.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                jtBuscarCitasKeyTyped(evt);
            }
        });

        jdtFechaABuscar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jdtFechaABuscarMouseClicked(evt);
            }
        });
        jdtFechaABuscar.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                jdtFechaABuscarPropertyChange(evt);
            }
        });

        javax.swing.GroupLayout jpEditarEliminarCitaLayout = new javax.swing.GroupLayout(jpEditarEliminarCita);
        jpEditarEliminarCita.setLayout(jpEditarEliminarCitaLayout);
        jpEditarEliminarCitaLayout.setHorizontalGroup(
            jpEditarEliminarCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jpEditarEliminarCitaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpEditarEliminarCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jtBuscarCitas)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jpEditarEliminarCitaLayout.createSequentialGroup()
                        .addGap(198, 198, 198)
                        .addGroup(jpEditarEliminarCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jSeparator3)
                            .addGroup(jpEditarEliminarCitaLayout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(jdtFechaABuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 214, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 737, Short.MAX_VALUE)
                    .addComponent(jlTipoContacto, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(jpEditarEliminarCitaLayout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jbEditar)
                        .addGap(18, 18, 18)
                        .addComponent(jbEliminar)))
                .addGap(33, 33, 33))
        );
        jpEditarEliminarCitaLayout.setVerticalGroup(
            jpEditarEliminarCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpEditarEliminarCitaLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jlTipoContacto)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jdtFechaABuscar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(11, 11, 11)
                .addComponent(jtBuscarCitas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 119, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jpEditarEliminarCitaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbEditar)
                    .addComponent(jbEliminar))
                .addContainerGap(262, Short.MAX_VALUE))
        );

        jtpAgenda.addTab("Edición", jpEditarEliminarCita);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jtpAgenda)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jtpAgenda, javax.swing.GroupLayout.PREFERRED_SIZE, 537, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jlAdelanteMesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlAdelanteMesMouseClicked
        // TODO add your handling code here:
        if (numeroMes < 12) {
            numeroMes++;
            numeroMesActual = numeroMes;
            inicializaCalendario();
        } else {
            numeroMes = 1;
            inicializaCalendario();
        }
    }//GEN-LAST:event_jlAdelanteMesMouseClicked

    private void jlAtrasMEsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jlAtrasMEsMouseClicked
        // TODO add your handling code here:
        if (numeroMes > 1) {
            numeroMes--;
            numeroMesActual = numeroMes;
            inicializaCalendario();
        } else {
            numeroMes = 12;
            inicializaCalendario();
        }
    }//GEN-LAST:event_jlAtrasMEsMouseClicked

    private void jpNuevaCitaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpNuevaCitaComponentShown

    }//GEN-LAST:event_jpNuevaCitaComponentShown

    private void jbCrearCitaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCrearCitaActionPerformed
        crearCita();
        limpiarCampos();
    }//GEN-LAST:event_jbCrearCitaActionPerformed

    private void jtfBuscarKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtfBuscarKeyTyped
        int filas = jtContactos.getRowCount();
        int columnas = jtContactos.getColumnCount();
        for (int fila = 0; fila < filas; fila++) {
            for (int columna = 0; columna < columnas; columna++) {
                String word = String.valueOf(jtContactos.getValueAt(fila,
                        columna));
                if (jtfBuscar.getText() == null ? word == null : jtfBuscar.getText().toLowerCase().equals(word.toLowerCase())) {
                    jtContactos.changeSelection(fila, columna, false, false);
                    columna = columnas;
                    fila = filas;
                }
            }
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jtfBuscarKeyTyped

    private void jspCitasComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jspCitasComponentShown
        cargaTablaContactos();
    }//GEN-LAST:event_jspCitasComponentShown

    private void jpContenedorCalendarioComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpContenedorCalendarioComponentShown

        // TODO add your handling code here:
    }//GEN-LAST:event_jpContenedorCalendarioComponentShown

    private void jpCalendarioComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpCalendarioComponentShown
        inicializaCalendario();          // TODO add your handling code here:
    }//GEN-LAST:event_jpCalendarioComponentShown

    private void jpMesComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpMesComponentShown
        inicializaCalendario();        // TODO add your handling code here:
    }//GEN-LAST:event_jpMesComponentShown

    private void jbRestablecerCamposActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbRestablecerCamposActionPerformed
        limpiarCampos();        // TODO add your handling code here:
    }//GEN-LAST:event_jbRestablecerCamposActionPerformed

    private void jcbTipoContactoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jcbTipoContactoActionPerformed
        if (evt.getSource() == jcbTipoContacto) {
            cargaTablaContactos();
        }        // TODO add your handling code here:
    }//GEN-LAST:event_jcbTipoContactoActionPerformed

    private void jpEditarEliminarCitaComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_jpEditarEliminarCitaComponentShown
        cargaTablaCitas();
    }//GEN-LAST:event_jpEditarEliminarCitaComponentShown

    private void jtBuscarCitasKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtBuscarCitasKeyTyped
        int filas = jtCitas.getRowCount();
        int columnas = jtCitas.getColumnCount();
        for (int fila = 0; fila < filas; fila++) {
            for (int columna = 0; columna < columnas; columna++) {
                String word = String.valueOf(jtCitas.getValueAt(fila,
                        columna));
                if (jtfBuscar.getText() == null ? word == null : jtfBuscar.getText().toLowerCase().equals(word.toLowerCase())) {
                    jtCitas.changeSelection(fila, columna, false, false);
                    columna = columnas;
                    fila = filas;
                }
            }
        }         // TODO add your handling code here:
    }//GEN-LAST:event_jtBuscarCitasKeyTyped

    private void jdtFechaABuscarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jdtFechaABuscarMouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jdtFechaABuscarMouseClicked

    private void jdtFechaABuscarPropertyChange(java.beans.PropertyChangeEvent evt) {//GEN-FIRST:event_jdtFechaABuscarPropertyChange
        try {
            fecha = jdtFechaABuscar.getDate();
            cargaTablaCitas();
        } catch (java.lang.NullPointerException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jdtFechaABuscarPropertyChange

    private void limpiarTabla() {
        DefaultTableModel modelo = (DefaultTableModel) jtContactos.getModel();
        while (jtContactos.getRowCount() > 0) {
            modelo.removeRow(0);
        }
        modelo = (DefaultTableModel) jtCitas.getModel();
        while (jtCitas.getRowCount() > 0) {
            modelo.removeRow(0);
        }
    }

    //Metodo usado para obtener la fecha actual
    //@return Retorna un <b>STRING</b> con la fecha actual formato "dd-MM-yyyy"
    public static Date getFechaActual() {
        Date ahora = new Date();
        SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //return formateador.format(ahora);
        return ahora;
    }

    public static Date formatoFecha(Date fecha) {
        Date ahora;
        SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
        ahora = deStringToDate(formateador.format(fecha));
        return ahora;
    }

    //Metodo usado para obtener la hora actual del sistema
    //@return Retorna un <b>STRING</b> con la hora actual formato "hh:mm:ss"
    public static Date getHoraActual() {
        Date ahora = new Date();
        SimpleDateFormat formateador = new SimpleDateFormat("hh:mm:ss");
        //return formateador.format(ahora);
        return ahora;
    }

    //Sumarle dias a una fecha determinada
    //@param fch La fecha para sumarle los dias
    //@param dias Numero de dias a agregar
    //@return La fecha agregando los dias
    public static java.sql.Date sumarFechasDias(java.sql.Date fch, int dias) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(fch.getTime());
        cal.add(Calendar.DATE, dias);
        return new java.sql.Date(cal.getTimeInMillis());
    }

    //Restarle dias a una fecha determinada
    //@param fch La fecha
    //@param dias Dias a restar
    //@return La fecha restando los dias
    public static synchronized java.sql.Date restarFechasDias(java.sql.Date fch, int dias) {
        Calendar cal = new GregorianCalendar();
        cal.setTimeInMillis(fch.getTime());
        cal.add(Calendar.DATE, -dias);
        return new java.sql.Date(cal.getTimeInMillis());
    }

    //Diferencias entre dos fechas
    //@param fechaInicial La fecha de inicio
    //@param fechaFinal  La fecha de fin
    //@return Retorna el numero de dias entre dos fechas
    public static synchronized int diferenciasDeFechas(Date fechaInicial, Date fechaFinal) {

        DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM);
        String fechaInicioString = df.format(fechaInicial);
        try {
            fechaInicial = df.parse(fechaInicioString);
        } catch (ParseException ex) {
        }

        String fechaFinalString = df.format(fechaFinal);
        try {
            fechaFinal = df.parse(fechaFinalString);
        } catch (ParseException ex) {
        }

        long fechaInicialMs = fechaInicial.getTime();
        long fechaFinalMs = fechaFinal.getTime();
        long diferencia = fechaFinalMs - fechaInicialMs;
        double dias = Math.floor(diferencia / (1000 * 60 * 60 * 24));
        return ((int) dias);
    }

    //Devuele un java.util.Date desde un String en formato dd-MM-yyyy
    //@param La fecha a convertir a formato date
    //@return Retorna la fecha en formato Date
    public static synchronized java.util.Date deStringToDate(String fecha) {
        SimpleDateFormat formatoDelTexto = new SimpleDateFormat("dd-MM-yyyy");
        Date fechaEnviar = null;
        try {
            fechaEnviar = formatoDelTexto.parse(fecha);
            return fechaEnviar;
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel5;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JSeparator jSeparator4;
    private javax.swing.JSeparator jSeparator5;
    private javax.swing.JButton jbCrearCita;
    private javax.swing.JButton jbEditar;
    private javax.swing.JButton jbEliminar;
    private javax.swing.JButton jbRestablecerCampos;
    private javax.swing.JComboBox jcbHoraCita;
    private javax.swing.JComboBox jcbMinutosCita;
    private javax.swing.JComboBox jcbTipoContacto;
    private com.toedter.calendar.JDateChooser jdtFechaABuscar;
    private com.toedter.calendar.JDateChooser jdtFechaCita;
    private javax.swing.JLabel jlAdelanteMes;
    private javax.swing.JLabel jlAtrasMEs;
    private javax.swing.JLabel jlDomingo;
    private javax.swing.JLabel jlHoraEvento;
    private javax.swing.JLabel jlHoraEvento1;
    private javax.swing.JLabel jlJueves;
    private javax.swing.JLabel jlListaContacto;
    private javax.swing.JLabel jlListaContacto1;
    private javax.swing.JLabel jlLunes;
    private javax.swing.JLabel jlMartes;
    private javax.swing.JLabel jlMiercoles;
    private javax.swing.JLabel jlNombreMes;
    private javax.swing.JLabel jlSabado;
    private javax.swing.JLabel jlTipoContacto;
    private javax.swing.JLabel jlViernes;
    private javax.swing.JPanel jpCalendario;
    private javax.swing.JPanel jpContenedorCalendario;
    private javax.swing.JPanel jpEditarEliminarCita;
    private javax.swing.JPanel jpMes;
    private javax.swing.JPanel jpNuevaCita;
    private javax.swing.JScrollPane jspCalendario;
    private javax.swing.JScrollPane jspCitas;
    private javax.swing.JTextField jtBuscarCitas;
    private javax.swing.JTable jtCitas;
    private javax.swing.JTable jtContactos;
    private javax.swing.JTextArea jtaDescripcion;
    private javax.swing.JTextField jtfBuscar;
    private javax.swing.JTabbedPane jtpAgenda;
    // End of variables declaration//GEN-END:variables

    private void cargaTablaContactos() {
        SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
        String fechaBuscar;
        fechaBuscar = formateador.format(fecha);

        ControlContacto co = new ControlContacto();
        List<Contacto> listaContactos
                = co.select("from Contacto where tipo ='"
                        + jcbTipoContacto.getSelectedItem().toString() + "' ");
        limpiarTabla();
        DefaultTableModel model = (DefaultTableModel) jtContactos.getModel();

        for (Contacto contacto : listaContactos) {
            model.addRow(new Object[]{contacto.getIdContacto(), contacto.getNombre(), contacto.getTipo()});
        }
        jtContactos.setModel(model);        // TODO add your handling code here:

    }

    private void cargaTablaCitas() {
        SimpleDateFormat formateador = new SimpleDateFormat("yyyy-MM-dd");
        String fechaBuscar;
        fechaBuscar = formateador.format(fecha);

        ControlCitas cc = new ControlCitas();
        List<Citas> listaCitas = cc.select("from Citas where fecha ='" + fechaBuscar + "'");
        DefaultTableModel model2 = (DefaultTableModel) jtCitas.getModel();
        limpiarTabla();
        for (Citas cita : listaCitas) {
            model2.addRow(new Object[]{cita.getIdCitas(),
                cita.getContacto().getNombre(), cita.getFecha(),
                cita.getHora(), cita.getMinuto()});
        }
        jtCitas.setModel(model2);
    }
}
